package com.tbsoft.core.process;

/**
 * @desc 消息处理器的抽象类
 * @author lizhenhua
 * 
 */
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class AbstractProcess implements Processor{
	/**
	 * @desc 消息关联ID，BAM监控使用
	 */
	public static final String MSG_CORRELATE_ID="msgCorrelateId";

	private static final Log log = LogFactory.getLog(AbstractProcess.class);
    public void process(Exchange exchange) {

        String error = (String) exchange.getProperty("error");

        if (error != null) {
            log.info("上一节点有错误");
            return;
        }

        try {
            execute(exchange);
            setMonitorCorrelateHeader(exchange);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            exchange.setProperty("error", true);
            exchange.getOut().setBody(ex.getMessage());
        }
    }
    /**
     * @设置监控的消息关联ID
     */
	public void setMonitorCorrelateHeader(Exchange exchange){
		Object correlateId=exchange.getIn().getHeader(MSG_CORRELATE_ID);
		if(correlateId != null && exchange.hasOut()){
			exchange.getOut().setHeader(MSG_CORRELATE_ID, correlateId);
		}
	}
	/**
	 * @desc 消息实际处理方法 由子类具体实现
	 * @param exchange
	 * @throws Exception
	 */
    public abstract void execute(Exchange exchange) throws Exception;

}